package za.co.chaotik.tron.bots;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import za.co.chaotik.tron.AbstractBot;
import za.co.chaotik.tron.Cycle;
import za.co.chaotik.tron.Position;
import za.co.chaotik.tron.exceptions.TronException;

public class RandomMoveBot extends AbstractBot {
	protected Random rng;
	
	public RandomMoveBot() {
		super();
		
		// Init RNG
		rng = new Random(new Date().getTime());
		
		// Set bot metadata
		this.name = "Random Move Bot";
		this.developer = "Matt Van Der Westhuizen";
		this.email = "matt@chaotik.co.za";
	}
	
	@Override
	public void playTurn() throws TronException {
		Cycle myCycle = map.getCycle(player);
		List<Position> moves = map.getValidMoves(myCycle);
		List<Position> nonLosingMoves = new ArrayList<Position>();
		
		// Make a list of moves that won't lose the game
		for (Position move : moves) {
			if (move.getValue() == null) {
				nonLosingMoves.add(move);
			}
		}
		
		// Randomly pick a non-losing move
		if (nonLosingMoves.size() > 0) {
			map.moveCycle(myCycle, nonLosingMoves.get(rng.nextInt(nonLosingMoves.size())));
		}
	}
}
